﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Cos.Core.Service;
using Cos.Module.Configuration;

namespace Alogient.Cameleon.Sdk
{
    public sealed class CMSettings
    {
        private CMSettings() { }

        const string URL_REWRITING_PARAM = "CM.EnableUrlRewriting";
        const string DEFAULT_NAVID = "CM.DefaultNavID";
        const string DEFAULT_CULTURECODE = "CM.DefaultCultureCode";
        const string ADMIN_URL = "CM.AdminUrl";
        const string MEDIA_URL = "CM.MediaUrl";
        const string FORM_MEDIA_URL = "FM.FormUploadPath";
        const string CACHE_TIME = "CM.CacheTimeInSeconds";  //TODO Rename to CM.CacheTime
        public const string _SMTP_SERVER = "CM.SMTPServer";
        private const string HTTPS_ADMIN_URL = "CM.HTTPSAdminUrl";

        private static bool? urlRewritingEnabled = null;
        private static int? defaultNavID = null;
        private static string defaultCultureCode = null;
        private static string adminUrl = null;
        private static string mediaUrl = null;
        private static int? cacheTime = null;
        private static string smtpServer = null;
        private static string formMediaUrl = null;
        private static string httpsAdminUrl = null;

        public static bool UrlRewritingEnabled
        {
            get
            {
                if (urlRewritingEnabled == null)
                {
                    var value = false;
                    bool.TryParse(ConfigurationManager.AppSettings[URL_REWRITING_PARAM], out value);
                    urlRewritingEnabled = value;
                }

                return urlRewritingEnabled == true;
            }
        }

        public static int DefaultNavID
        {
            get
            {
                if (defaultNavID == null)
                {
                    int value;
                    int.TryParse(ConfigurationManager.AppSettings[DEFAULT_NAVID], out value);
                    if (value > 0)
                    {
                        defaultNavID = value;
                    }
                }

                return defaultNavID.HasValue ? defaultNavID.Value : 1;
            }
        }

        public static string DefaultCultureCode
        {
            get
            {
                if (defaultCultureCode == null)
                {
                    defaultCultureCode = ConfigurationManager.AppSettings[DEFAULT_CULTURECODE];

                    if (defaultCultureCode == null)
                    {
                        var cultureService = ServiceLocator.Get<ICultureService>();
                        defaultCultureCode = cultureService.DefaultCulture.CultureCode;
                    }
                }

                return defaultCultureCode;
            }
        }

        public static string AdminUrl
        {
            get
            {
                if (adminUrl == null)
                {
                    var value = ConfigurationManager.AppSettings[ADMIN_URL];

                    adminUrl = value ?? "admin";
                }

                return adminUrl;
            }
        }

        public static string MediaUrl
        {
            get
            {
                if (mediaUrl == null)
                {
                    var value = ConfigurationManager.AppSettings[MEDIA_URL];

                    if (value == null)
                    {
                        mediaUrl = "~/CMS/Media/";
                    }
                    else
                    {
                        mediaUrl = value;
                    }
                }

                return mediaUrl;
            }
        }

        public static string FormMediaUrl
        {
            get
            {
                if (formMediaUrl == null)
                {
                    var value = ConfigurationManager.AppSettings[FORM_MEDIA_URL];

                    formMediaUrl = value ?? "~/CMS/FormMedia/";
                }

                return formMediaUrl;
            }
        }

        /// <summary>
        /// Get page cache time in seconds
        /// </summary>
        /// <returns>Cache time from configuration file, defaults to 5 seconds</returns>
        public static int CacheTime
        {
            get
            {
                if (cacheTime == null)
                {
                    var value = 0;
                    int.TryParse(ConfigurationManager.AppSettings[CACHE_TIME], out value);
                    cacheTime = value;
                }

                return (int)cacheTime;
            }
        }

        public static string SMTPServer
        {
            get
            {
                if (smtpServer == null)
                {
                    smtpServer = ConfigurationManager.AppSettings[_SMTP_SERVER];
                }

                return smtpServer;
            }
        }

        public static string HttpsAdminUrl
        {
            get
            {
                if (httpsAdminUrl == null)
                {
                    var value = ConfigurationManager.AppSettings[HTTPS_ADMIN_URL];

                    httpsAdminUrl = value ?? "admin";
                }

                return httpsAdminUrl;
            }
        }
    }
}
